const http = require("http");
const URL = require("url");
const querystring = require('querystring');


//get请求
// uri参数提前拼好
//@param ip:string  localhost/127.0.0.1/192.168.0.1
//@param portstring 端口27000
//@param uri:string 访问路径  Api/PvfUtiltiy/getVersion
const httpGet = (ip, port, uri) => new Promise((reslove, rej) => {
    if (!ip || !port) {
        return
    }

    if (uri.indexOf("/") == 0) {
        uri = uri.substring(1);
    }

    let url = `http://${ip}:${port}/${uri}`;
    console.log("httpGet url=", url);

    http.get(url, res => {
        res.setEncoding('utf8');
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        res.on('end', () => {
            reslove(data);
        });
    }).on('error', (err) => {
        rej(err);
    });
});

//post请求
// uri参数提前拼好
//@param ip:string  localhost/127.0.0.1/192.168.0.1
//@param portstring 端口27000
//@param uri:string 访问路径  Api/PvfUtiltiy/getVersion
//@param params:string post数据包,如果是对象请转成json字符串单行。
const httpPost = (ip, port, uri, params) => new Promise((reslove, rej) => {
    if (!ip || !port) {
        return
    }

    if (uri.indexOf("/") == 0) {
        uri = uri.substring(1);
    }

    let url = `http://${ip}:${port}/${uri}`;
    console.log("httpPost url=", url, params);

    let req = http.request(url, { method: 'post' }, function (res) {
        let data = "";
        // console.log('status: ' + res.statuscode);
        // console.log('headers: ' + json.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            data += chunk
        });
        res.on('end', () => {
            reslove(data);
        });
    });
    req.on('error', err => {
        rej(err);
    });
    req.write(params);
    req.end();
});



//同步版 httpGet
async function syncHttpGet(ip, port, uri) {
    if (!ip || !port) {
        return
    }

    if (uri.indexOf("/") == 0) {
        uri = uri.substring(1);
    }

    let url = `http://${ip}:${port}/${uri}`;
    console.log("syncHttpGet url=", url);

    let data;
    await httpGet(ip, port, url).then(res => {
        data = res;
    }).catch(e => {
        data = e.toString();
    });
    return data;
}

//同步版 httpPost
async function syncHttpPost(ip, port, uri, params) {
    if (!ip || !port) {
        return
    }

    if (uri.indexOf("/") == 0) {
        uri = uri.substring(1);
    }

    let url = `http://${ip}:${port}/${uri}`;
    console.log("syncHttpPost url=", url, params);

    let data = "";

    await new Promise((res, rej) => {
        http.request(url, { method: 'post' }, function (res) {
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                data += chunk
            });
            res.on('end', () => {
                res();
            });
        });
        req.on('error', err => {
            rej(err);
        });
        req.write(params);
        req.end();
    })

    return data;
}

module.exports = {
    httpGet,
    httpPost,
    syncHttpGet,
    syncHttpPost,
}